﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace solitaire
{
    public partial class CardColumn : UserControl, ICardContainer
    {
        private List<Card> _cards = new List<Card>();

        public CardColumn()
        {
            InitializeComponent();
            this.Height = 124 + (25 * 12);
        }

        public Card BottomCard
        {
            get
            {
                return (_cards.Count > 0) ? _cards[0] : null;
            }
        }

        private int TopCardIndex
        {
            get { return _cards.Count - 1; }
        }

        private void UpdateCardView()
        {
            ClearAllCardsFromUI();
            int i = 0;
            int zi = 1;
            foreach (Card c in _cards)
            {
                Canvas.SetZIndex(c, zi++);
                Canvas.SetLeft(c, 0);
                Canvas.SetTop(c, i);
                if (LayoutRoot.Children.Count > 0)
                {
                    if (LayoutRoot.Children.Last() is Card && ((Card)LayoutRoot.Children.Last()).FaceUp)
                    {
                        Canvas.SetTop(c, i += 25);
                        c.DragEnabled = true;
                    }
                    else
                    {
                        Canvas.SetTop(c, i += 7);
                    }
                }
                LayoutRoot.Children.Add(c);
            }
        }

        internal void FlipBottomCardUp()
        {
            if (_cards.Count > 0 && !_cards[TopCardIndex].FaceUp)
            {
                _cards[TopCardIndex].FaceUp = true;
                _cards[TopCardIndex].DragEnabled = true;
            }
        }

        private void ClearAllCardsFromUI()
        {
            List<Card> killList = new List<Card>();

            foreach (UIElement eu in LayoutRoot.Children)
            {
                if (eu is Card) killList.Add((Card)eu);
            }
            while (killList.Count > 0)
            {
                LayoutRoot.Children.Remove(killList[0]);
                killList.RemoveAt(0);
            }
        }

        public void AddCardNoContainerUpdate(Card c)
        {
            _cards.Add(c);
            UpdateCardView();
        }

        #region ICardContainer Members
        public void AddCard(Card c)
        {
            c.FromContainer = this;
            _cards.Add(c);
            UpdateCardView();
        }

        public Card RetrieveTopCard()
        {
            if (TopCardIndex > 0)
            {
                Card nc = _cards[TopCardIndex].Clone();
                _cards.RemoveAt(TopCardIndex);
            }
            return null;
        }

        /// <summary>
        /// Determines if a candidate card is the correct card for placement in this card column
        /// </summary>
        /// <param name="c">The Card to Evaluate</param>
        /// <returns></returns>
        public bool IsGoodCardCandidate(Card c)
        {
            if (_cards.Count == 0)
            {
                return (c.CardValue != CardValue.King) ? false : true;
            }
            else
            {
                Card tc = _cards[TopCardIndex];
                if (tc.SuitColor == c.SuitColor) return false;
                if (((int)tc.CardValue - (int)c.CardValue) != 79) return false;
                return true;
            }
        }

        public Card RetrieveCard(Card c)
        {
            Card cc = c.Clone();
            _cards.Remove(c);
            UpdateCardView();
            return cc;
        }

        public Card[] RetrieveCardAndDescendants(Card c)
        {
            if (!_cards.Contains(c)) return null;
            int i = _cards.IndexOf(c);
            Card[] set = new Card[TopCardIndex - i + 1];

            int index = 0;
            for (int ii = i; ii <= TopCardIndex; ii++)
            {
                set[index++] = _cards[ii].Clone();
            }
            int tc = TopCardIndex;
            for (int ii = i; ii <= tc; ii++)
            {
                _cards.RemoveAt(i);
            }
            UpdateCardView();
            return set;
        }

        public bool IsInside(Point p)
        {
            if (p.X < Canvas.GetLeft(this) || p.X > Canvas.GetLeft(this) + this.Width) return false;
            if (p.Y < Canvas.GetTop(this) || p.Y > Canvas.GetTop(this) + this.Height) return false;
            return true;
        }

        public int TotalCards
        {
            get { return _cards.Count; }
        }

        public void Clear()
        {
            ClearAllCardsFromUI();
            _cards.Clear();
        }

        #endregion
    }
}
